namespace cnVery.bbs.Handlers
{
    using cnVery.Utils;
    using System;
    using System.Xml;

    public class StatisticHandler
    {
        public static void AddToday(string elementName, int augend)
        {
            if (DateTime.Now.ToString("yyyy-MM-dd") != DateTime.Parse(GetValue("Today")).ToString("yyyy-MM-dd"))
            {
                SetValue("Today", DateTime.Now.ToString("yyyy-MM-dd"));
                SetValue("YestodayTopics", GetValue("TodayTopics"));
                SetValue("TodayTopics", "0");
            }
            Calculate(elementName, 1);
        }

        public static void Calculate(string elementName, int augend)
        {
            XmlDocument document = new XmlDocument();
            document.Load(path);
            document.GetElementsByTagName(elementName)[0].InnerText = (int.Parse(document.GetElementsByTagName(elementName)[0].InnerText) + augend).ToString();
            document.Save(path);
        }

        public static string GetValue(string elementName)
        {
            XmlDocument document = new XmlDocument();
            document.Load(path);
            return document.GetElementsByTagName(elementName)[0].InnerText;
        }

        public static string[] GetValues(string[] elementNames)
        {
            XmlDocument document = new XmlDocument();
            document.Load(path);
            int length = elementNames.Length;
            string[] strArray = new string[length];
            for (int i = 0; i < length; i++)
            {
                strArray[i] = document.GetElementsByTagName(elementNames[i])[0].InnerText;
            }
            return strArray;
        }

        public static void SetValue(string elementName, string value)
        {
            XmlDocument document = new XmlDocument();
            document.Load(path);
            document.GetElementsByTagName(elementName)[0].InnerText = value;
            document.Save(path);
        }

        public static void SetValues(string[] elementNames, string[] values)
        {
            int length = elementNames.Length;
            if (length == values.Length)
            {
                XmlDocument document = new XmlDocument();
                document.Load(path);
                for (int i = 0; i < length; i++)
                {
                    document.GetElementsByTagName(elementNames[i])[0].InnerText = values[i];
                }
                document.Save(path);
            }
        }

        private static string path
        {
            get
            {
                return Fetch.MapPath("files/localfiles/statistic.xml");
            }
        }
    }
}

